package logic

import (
	"context"
	"fmt"
	"strings"

	"mall/common/utils/pointy"
	"mall/service/recommended/rpc/internal/svc"
	"mall/service/recommended/rpc/types/recommended"

	"github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetNeighborsListByItemIdCategoryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetNeighborsListByItemIdCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetNeighborsListByItemIdCategoryLogic {
	return &GetNeighborsListByItemIdCategoryLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 根据itemid  Category 获取相似度列表
func (l *GetNeighborsListByItemIdCategoryLogic) GetNeighborsListByItemIdCategory(in *recommended.NeighborsListReq) (*recommended.NeighborsListResp, error) {
	if in.ItemId == nil || len(strings.TrimSpace(*in.ItemId)) == 0 {

	}
	logrus.Info(fmt.Sprintf("GetNeighborsListByItemIdCategory ItemID: %+v", *in.ItemId))
	var category string
	var pageSize uint64 = 1
	var page uint64 = 1
	if in.Category != nil && len(strings.TrimSpace(*in.Category)) >= 0 {
		category = *in.Category
	}
	if in.Limit != nil && *in.Limit > 0 {
		pageSize = *in.Limit
	}
	if in.Office != nil && *in.Office > 0 {
		page = *in.Office
	}
	res, err := l.svcCtx.GorseClient.GetItemNeighborsByCategory(l.ctx, *in.ItemId, category, int(pageSize), int(page))
	if err != nil {
		return nil, err
	}
	var resp = &recommended.NeighborsListResp{}
	if len(res) > 0 {
		for _, v := range res {
			resp.List = append(resp.List, &recommended.ScoreInfo{
				Id:    pointy.GetPointer(v.Id),
				Score: pointy.GetPointer(float32(v.Score)),
			})
		}
	}
	return resp, nil
}
